import {Component, OnDestroy, OnInit} from '@angular/core';
import {ProjectService} from "../project.service";
import {ActivatedRoute, Router} from "@angular/router";
import {Location} from "@angular/common";
import * as $ from "jquery";
import {WebsocketService} from "../../../public/service/websocket.service";
import {SettingUrl} from "../../../public/setting/setting_url";

@Component({
  selector: 'app-log-page',
  templateUrl: './log-page.component.html',
  styleUrls: ['./log-page.component.css']
})
export class LogPageComponent implements OnInit, OnDestroy {
  public logInfo: any = [];          //日志信息
  public code: string;                      //项目编码
  public taskCode: string;                  //任务编码
  public timer: any;                        //定时器
  public home: any;                         //仓库的地址,当检测到日志打印完之后跳转到仓库的地址
  public state: any;                        //构建历史状态
  public websocketUrl = SettingUrl.URL.base.websocket;//本地的websocket地址

  constructor(public projectService: ProjectService,
              public router: Router,
              public location: Location,
              public routerInfo: ActivatedRoute,
              private wsService: WebsocketService) {
  }

  ngOnInit() {
    let me = this;
    me.code = me.routerInfo.snapshot.queryParams['code'];
    me.taskCode = me.routerInfo.snapshot.queryParams['taskCode'];
    me.home = me.routerInfo.snapshot.queryParams['home'];
    me.state = me.routerInfo.snapshot.queryParams['state'];
    me.websocket();
  }

  websocket() {
    this.wsService
      .createObservableSocket(this.websocketUrl)
      .subscribe(data => {
          if (data.open) {
            this.getLog();
          } else if (JSON.stringify(data).indexOf('End') != -1) {
            this.wsService.closeWs()
          } else this.logInfo.push(data);
          this.scrollBottom();
        },
        err => console.log(err),
        () => console.log("流已经结束")
      );
  }

  /**
   * 通过使页面底部的一个元素实施滚动到页面可见部分来达到页面跟随日志滚动的目的
   */
  scrollBottom() {
    let logBottom = document.getElementById('log-bottom');
    logBottom.scrollIntoView();
  }

  sendMessage() {
    this.wsService.sendMessage(this.websocketUrl, '111111111111111111111111')
  }

  /**
   * 组件销毁的时候执行的发方法
   */
  ngOnDestroy() {
    let me = this;
    me.wsService.closeWs();
  }

  /**
   * 执行任务生成任务编码，如果构建历史跳转过来的话,就有任务编码直接调用打印日志的方法
   */

  getLog() {
    let me = this;
    if (me.taskCode) {
      me.continueRequest(me.taskCode);
      return;
    }
    let data = {
      code: this.code
    };
    $.when(this.projectService.excuteTask(data)).always(result => {
      if (result) {
        let taskCode = result.code;
      }
    });
  }

  /**
   * 持续的请求生成日志
   * @param taskCode
   */
  continueRequest(taskCode) {
    let me = this;
    if (me.state) {
      let size: string;
      if (me.state == 'Completed' || me.state == 'Error' || me.state == 'Waring') {
        let size = '999';
        let init: number = 0;
        let data = {
          curPage: ++init,
          pageSize: size,
          code: taskCode,//任务编码
        };
        $.when(me.projectService.getLogsList(data)).always(data => {
          sessionStorage.setItem('code', me.code);
          me.logInfo.push(data.voList);
        });
      } else if (me.state == 'Create' || me.state == 'Executing') {
        me.timer = setInterval(() => {
          let size = '20';
          let init: number = 0;
          let data = {
            curPage: ++init,
            pageSize: init == 1 ? size : 3,
            code: taskCode,//任务编码
          };
          $.when(me.projectService.getLogsList(data)).always(data => {
            sessionStorage.setItem('code', me.code);
            me.logInfo.push(data.voList);
            me.linkHome(data.voList);
          });
        }, 2000);
      }
    } else {
      let init: number = 0;
      me.timer = setInterval(() => {
        let data = {
          curPage: ++init,
          pageSize: sessionStorage.getItem('code') == me.code ? init == 1 ? 20 : 3 : 3,
          code: taskCode,//任务编码
        };
        $.when(me.projectService.getLogsList(data)).always(data => {
          sessionStorage.setItem('code', me.code);
          me.logInfo.push(data.voList);
          me.linkHome(data.voList);
        });
      }, 2000);
    }
  }

  /**
   * 判断是否结束跳转到仓库页面
   */
  linkHome(data) {
    let me = this;
    for (let i = 0; i < data.length; i++) {
      if (data[i].log == 'End') {
        window.open(me.home);
        window.clearInterval(me.timer);
      }
    }
  }

  /**
   * 返回
   */
  goBack() {
    this.location.back();
  }
}
